package com.dianping.cat.consumer.heartbeat.model.transform;

import com.dianping.cat.consumer.heartbeat.model.IEntity;
import com.dianping.cat.consumer.heartbeat.model.IVisitor;
import com.dianping.cat.consumer.heartbeat.model.entity.Detail;
import com.dianping.cat.consumer.heartbeat.model.entity.Disk;
import com.dianping.cat.consumer.heartbeat.model.entity.Extension;
import com.dianping.cat.consumer.heartbeat.model.entity.HeartbeatReport;
import com.dianping.cat.consumer.heartbeat.model.entity.Machine;
import com.dianping.cat.consumer.heartbeat.model.entity.Period;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;





































public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  62 */   public DefaultXmlBuilder() { this(false); }



/*  66 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  70 */     this.m_compact = compact;
/*  71 */     this.m_sb = sb;
/*  72 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  76 */     entity.accept(this.m_visitor);
/*  77 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  81 */     this.m_level--;

/*  83 */     indent();
/*  84 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  88 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  92 */     if (value == null) {
/*  93 */       return null;
    }

/*  96 */     String str = toString(value);
/*  97 */     int len = str.length();
/*  98 */     StringBuilder sb = new StringBuilder(len + 16);

/* 100 */     for (int i = 0; i < len; i++) {
/* 101 */       char ch = str.charAt(i);

/* 103 */       switch (ch) {
        case '<':
/* 105 */           sb.append("&lt;");
          break;
        case '>':
/* 108 */           sb.append("&gt;");
          break;
        case '&':
/* 111 */           sb.append("&amp;");
          break;
        case '"':
/* 114 */           if (!text) {
/* 115 */             sb.append("&quot;");
            break;
          }
        default:
/* 119 */           sb.append(ch);
          break;
      }

    }
/* 124 */     return sb.toString();
  }

  protected void indent() {
/* 128 */     if (!this.m_compact) {
/* 129 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 130 */         this.m_sb.append("   ");
      }
    }
  }


/* 136 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 140 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 144 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 148 */     indent();

/* 150 */     this.m_sb.append('<').append(name);

/* 152 */     int len = nameValues.length;

/* 154 */     for (int i = 0; i + 1 < len; i += 2) {
/* 155 */       Object attrName = nameValues[i];
/* 156 */       Object attrValue = nameValues[i + 1];

/* 158 */       if (attrValue != null) {
/* 159 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 163 */     if (dynamicAttributes != null) {
/* 164 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 165 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 169 */     if (text != null && closed) {
/* 170 */       this.m_sb.append('>');
/* 171 */       this.m_sb.append(escape(text, true));
/* 172 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 174 */       if (closed) {
/* 175 */         this.m_sb.append('/');
      } else {
/* 177 */         this.m_level++;
      }

/* 180 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 186 */     if (value instanceof String)
/* 187 */       return (String)value; 
/* 188 */     if (value instanceof Collection) {
/* 189 */       Collection<Object> list = (Collection)value;
/* 190 */       StringBuilder sb = new StringBuilder(32);
/* 191 */       boolean first = true;

/* 193 */       for (Object item : list) {
/* 194 */         if (first) {
/* 195 */           first = false;
        } else {
/* 197 */           sb.append(',');
        }

/* 200 */         if (item != null) {
/* 201 */           sb.append(item);
        }
      }

/* 205 */       return sb.toString();
/* 206 */     }  if (value.getClass().isArray()) {
/* 207 */       int len = Array.getLength(value);
/* 208 */       StringBuilder sb = new StringBuilder(32);
/* 209 */       boolean first = true;

/* 211 */       for (int i = 0; i < len; i++) {
/* 212 */         Object item = Array.get(value, i);

/* 214 */         if (first) {
/* 215 */           first = false;
        } else {
/* 217 */           sb.append(',');
        }

/* 220 */         if (item != null) {
/* 221 */           sb.append(item);
        }
      }

/* 225 */       return sb.toString();
    }

/* 228 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 232 */     if (text == null) {
      return;
    }

/* 236 */     indent();

/* 238 */     this.m_sb.append('<').append(name);

/* 240 */     int len = nameValues.length;

/* 242 */     for (int i = 0; i + 1 < len; i += 2) {
/* 243 */       Object attrName = nameValues[i];
/* 244 */       Object attrValue = nameValues[i + 1];

/* 246 */       if (attrValue != null) {
/* 247 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 251 */     this.m_sb.append(">");
/* 252 */     this.m_sb.append(escape(text, true));
/* 253 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 257 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 261 */     indent();

/* 263 */     this.m_sb.append('<').append(name).append(">");

/* 265 */     if (escape) {
/* 266 */       this.m_sb.append(escape(text, true));
    } else {
/* 268 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 271 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 275 */     if (date != null) {
/* 276 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 278 */     return null;
  }


  protected String toString(Number number, String format) {
/* 283 */     if (number != null) {
/* 284 */       return (new DecimalFormat(format)).format(number);
    }
/* 286 */     return null;
  }




/* 292 */   public void visitDetail(Detail detail) { startTag("detail", true, null, new Object[] { "id", detail.getId(), "value", Double.valueOf(detail.getValue()) }); }




/* 297 */   public void visitDisk(Disk disk) { startTag("disk", true, null, new Object[] { "path", disk.getPath(), "total", Long.valueOf(disk.getTotal()), "free", Long.valueOf(disk.getFree()), "usable", Long.valueOf(disk.getUsable()) }); }



  public void visitExtension(Extension extension) {
/* 302 */     startTag("extension", null, new Object[] { "id", extension.getId() });

/* 304 */     if (!extension.getDetails().isEmpty()) {
/* 305 */       for (Detail detail : extension.getDetails().values()) {
/* 306 */         detail.accept(this.m_visitor);
      }
    }

/* 310 */     endTag("extension");
  }


  public void visitHeartbeatReport(HeartbeatReport heartbeatReport) {
/* 315 */     startTag("heartbeat-report", null, new Object[] { "domain", heartbeatReport.getDomain(), "startTime", toString(heartbeatReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(heartbeatReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 317 */     if (!heartbeatReport.getDomainNames().isEmpty()) {
/* 318 */       for (String domain : heartbeatReport.getDomainNames()) {
/* 319 */         tagWithText("domain", domain, new Object[0]);
      }
    }

/* 323 */     if (!heartbeatReport.getIps().isEmpty()) {
/* 324 */       for (String ip : heartbeatReport.getIps()) {
/* 325 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 329 */     if (!heartbeatReport.getMachines().isEmpty()) {
/* 330 */       for (Machine machine : heartbeatReport.getMachines().values()) {
/* 331 */         machine.accept(this.m_visitor);
      }
    }

/* 335 */     endTag("heartbeat-report");
  }


  public void visitMachine(Machine machine) {
/* 340 */     startTag("machine", null, new Object[] { "ip", machine.getIp(), "classpath", machine.getClasspath() });

/* 342 */     if (!machine.getPeriods().isEmpty()) {
/* 343 */       for (Period period : machine.getPeriods()) {
/* 344 */         period.accept(this.m_visitor);
      }
    }

/* 348 */     endTag("machine");
  }


  public void visitPeriod(Period period) {
/* 353 */     startTag("period", period.getDynamicAttributes(), new Object[] { "minute", Integer.valueOf(period.getMinute()), "thread-count", Integer.valueOf(period.getThreadCount()), "daemon-count", Integer.valueOf(period.getDaemonCount()), "total-started-count", Integer.valueOf(period.getTotalStartedCount()), "cat-thread-count", Integer.valueOf(period.getCatThreadCount()), "pigeon-thread-count", Integer.valueOf(period.getPigeonThreadCount()), "http-thread-count", Integer.valueOf(period.getHttpThreadCount()), "new-gc-count", Long.valueOf(period.getNewGcCount()), "old-gc-count", Long.valueOf(period.getOldGcCount()), "memory-free", Long.valueOf(period.getMemoryFree()), "heap-usage", Long.valueOf(period.getHeapUsage()), "none-heap-usage", Long.valueOf(period.getNoneHeapUsage()), "system-load-average", toString(Double.valueOf(period.getSystemLoadAverage()), "0.00"), "cat-message-produced", Long.valueOf(period.getCatMessageProduced()), "cat-message-overflow", Long.valueOf(period.getCatMessageOverflow()), "cat-message-size", toString(Double.valueOf(period.getCatMessageSize()), "0.00") });

/* 355 */     if (!period.getDisks().isEmpty()) {
/* 356 */       for (Disk disk : period.getDisks()) {
/* 357 */         disk.accept(this.m_visitor);
      }
    }

/* 361 */     if (!period.getExtensions().isEmpty()) {
/* 362 */       for (Extension extension : period.getExtensions().values()) {
/* 363 */         extension.accept(this.m_visitor);
      }
    }

/* 367 */     endTag("period");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/heartbeat/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */